package dao;

import static org.junit.Assert.*;

import java.sql.SQLException;
import java.util.Vector;

import org.junit.Test;
import org.junit.BeforeClass;

import java.sql.SQLException;

import org.junit.BeforeClass;
import org.junit.Test;

public class HabitatTest {

	
	
@BeforeClass
public static void test_create()
	{
		//CreateTables createTables = new CreateTables();
        //createTables.CreateTablesMethod("birddb");

        try {
                ConnectionManager.connect("birddb");
        } catch(SQLException ex) {
                System.out.println("Connection failed!");
        } catch(ClassNotFoundException ex){
                System.out.println("No driver found, " +
                                   "check your CLASSPATH!");
        }
	}
	
	@Test
	public void testInsert() 
	{
		Habitat hab = new Habitat("alta","Ilha",30,20,"Dumberlandia");
		
		try {
			hab.insert();
		} catch (SQLException e) {
			System.out.println("Error at insertion: "+e);
		}
		
		System.out.println("Habitat insertion ok!");
	}

	@Test
	public void testRemove() 
	{
		Habitat hab = new Habitat("alta","Ilha",30,20,"Dumberlandia");
		
		try {
			hab.remove();
		} catch (SQLException e) {
			System.out.println("Error while removing: "+e);

		}
		
		System.out.println("Habitat removal ok");
	}

	@Test
	public void testUpdate() {
		Habitat hab = new Habitat("alta","Ilha",10,5,"Dumberlandia");
		
		try {
			hab.update();
		} catch (SQLException e) {
			System.out.println("Error while updating: "+e);

		}
		
		System.out.println("Habitat update ok");
	}

	@Test
	public void testListAll() {
		Vector<Habitat> lista = new Vector<Habitat>();
		
		Habitat hab = new Habitat("alta","Montanha",30,20,"Unicamp");
		try {
			hab.insert();
		} catch (SQLException e) {
			System.out.println("Error at insertion: "+e);
		}
		
		try {
			   lista = Habitat.listAll();
		} catch (SQLException e) {
			System.out.println("Error when listing all habitats:"+e);
		}
		
		assertNotNull("NULL",lista);
        assertTrue("Not found",lista.size() >= 1);
        
        System.out.println("Listing all good!");
		
	}

	@Test
	public void testAssociateBird() {
		Bird bird = new Bird("passarus voante", 10, "low", 15,
				 	 		 "passaro que voa muito alto", "mapa1.png");
		Habitat hab = new Habitat("alta","Ilha",10,5,"Dumberlandia");
		
		try {
			hab.associateBird(bird);
		} catch (SQLException e) {
			System.out.println("Error while associating bird to habitat:"+e);
		}
		
		System.out.println("Association ok !");
	}

	@Test
	public void testDesassociateBird() {
		Bird bird = new Bird("passarus voante", 10, "low", 15,
	 	 		 			 "passaro que voa muito alto", "mapa1.png");
		Habitat hab = new Habitat("alta","Ilha",10,5,"Dumberlandia");
		
		Boolean b = hab.desassociateBird(bird);
		assertEquals("Desassociation is wrong!",b,true);
		
		System.out.println("Desassociation has been done!");
		
	}

}
